﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Grassroots;

namespace SysManage.Module.Grassroots
{
    public partial class GroupEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "GrassrootsGroupAdd";
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsGroup current = DB.PartyGrassrootsGroups.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtPageName.Text = current.GroupName;
                //txtX.Text = current.CssPointX.ToString();
                //txtY.Text = current.CssPointY.ToString();
                txtOrderNo.Text = current.OrderNo.ToString();
                cbxEnabled.Checked = current.State == 4;
            }
            else
            {
                var counts = DB.PartyGrassrootsGroups.ToList().Count+1;
                txtOrderNo.Text = counts.ToString();
            }
        }

        #endregion

        #region Events

        private void SaveData()
        {
            PartyGrassrootsGroup item;
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                //更新
                item = DB.PartyGrassrootsGroups.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                var current = DB.PartyGrassrootsGroups.Where(a=>a.GroupName== this.txtPageName.Text.Trim());
                if (current.Any())
                {
                    Alert.Show("名称已存在，请重新输入！", String.Empty);
                    return;
                }
                item = new PartyGrassrootsGroup();
                item.ID = StringHelper.NewID;
                item.BranchID = "";
                item.Description = "";
                item.CreateTime = DateTime.Now;
                item.ModifyTime = DateTime.Now;
                item.CreateBy = CurrentUser.LoginID;
                item.ModifyBy = CurrentUser.LoginID;
                DB.PartyGrassrootsGroups.Add(item);
            }

            item.OrderNo = Convert.ToInt32(txtOrderNo.Text);
            item.GroupName = txtPageName.Text;
            item.State = cbxEnabled.Checked?4:1;
            //item.CssPointX = Convert.ToInt32(txtX.Text);
            //item.CssPointY = Convert.ToInt32(txtY.Text);
            DB.SaveChanges();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();
            
        }
        #endregion

    }
}
